package com.ysy.tz.mfg.controller;

import com.github.pagehelper.PageInfo;
import com.sun.javafx.application.PlatformImpl;
import com.ysy.tz.core.annotation.Service;
import com.ysy.tz.core.annotation.ShowLoading;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.engine.CustomerFXMLLoader;
import com.ysy.tz.core.selfcomp.CommonDialog;
import com.ysy.tz.core.selfcomp.CusomterTableView;
import com.ysy.tz.core.selfcomp.CustomerPagination;
import com.ysy.tz.core.selfcomp.NoHeaderTableView;
import com.ysy.tz.core.utils.FormUtils;
import com.ysy.tz.core.utils.ModelUtils;
import com.ysy.tz.core.utils.PrintUtils;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.dao.DataListDao;
import com.ysy.tz.mfg.dao.SmartEquipmentDao;
import com.ysy.tz.mfg.dto.ArriveFactoryDto;
import com.ysy.tz.mfg.entity.ArriveFactoryEntity;
import com.ysy.tz.mfg.entity.DataListEntity;
import com.ysy.tz.mfg.entity.FlightsEntity;
import com.ysy.tz.mfg.entity.SmartEquipmentEntity;
import com.ysy.tz.mfg.service.ArriveFactoryService;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.AccessibleAttribute;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javax.imageio.ImageIO;

public class ArriveFactoryController implements Initializable {
    @FXML
    private ComboBox flightsCode;
    @FXML
    private ComboBox selfSupplyCode;
    @FXML
    private ComboBox smartCode;
    @FXML
    private BorderPane queryForm;
    @FXML
    private CusomterTableView arriveFactoryTableView;
    @FXML
    private NoHeaderTableView sumArriveFactoryTableView;
    @FXML
    private CustomerPagination arriveFactoryTablePagination;
    @Service
    ArriveFactoryService arriveFactoryService;

    public ArriveFactoryController() {
    }

    public void initialize(URL location, ResourceBundle resources) {
        ArrayList<FlightsEntity> flightsList = (ArrayList)SessionUtils.getAttribute(SessionUtils.FLIGHTS);
        this.flightsCode.setItems(FXCollections.observableArrayList(flightsList));
        SQLUtils.executeSQL(DataListDao.class, (dataListDao) -> {
            DataListEntity dataListEntity = new DataListEntity();
            dataListEntity.setListTypeCode("SelfSupply");
            this.selfSupplyCode.setItems(FXCollections.observableArrayList(dataListDao.searchDataListByTypeCode(dataListEntity)));
        });
        SQLUtils.executeSQL(SmartEquipmentDao.class, (smartEquipmentDao) -> {
            SmartEquipmentEntity smartEquipmentEntity = new SmartEquipmentEntity();
            smartEquipmentEntity.setSmartTypeCode("XZK");
            this.smartCode.setItems(FXCollections.observableArrayList(smartEquipmentDao.searchSamrtEquipmentByTypeCode(smartEquipmentEntity)));
        });
    }

    public void reset() {
        FormUtils.resetFrom(this.queryForm);
    }

    @ShowLoading
    public void query() {
        try {
            ArriveFactoryDto condition = (ArriveFactoryDto)FormUtils.collectParams(this.queryForm, ArriveFactoryDto.class);
            ObservableList tableData = FXCollections.observableArrayList(this.arriveFactoryService.searchArriveFactorySum(condition));
            this.sumArriveFactoryTableView.setItems(tableData);
            condition.setPageNum(this.arriveFactoryTablePagination.getCurrentPageIndex() + 1);
            condition.setPageSize(this.arriveFactoryTablePagination.getPageSize());
            PageInfo<ArriveFactoryEntity> arriveFactoryList = this.arriveFactoryService.searchArriveFactoryList(condition);
            ModelUtils.setTableViewValue(arriveFactoryList, this.arriveFactoryTableView, this.arriveFactoryTablePagination);
            ScrollBar scrollBar1 = (ScrollBar)this.sumArriveFactoryTableView.queryAccessibleAttribute(AccessibleAttribute.HORIZONTAL_SCROLLBAR, new Object[0]);
            ScrollBar scrollBar2 = (ScrollBar)this.arriveFactoryTableView.queryAccessibleAttribute(AccessibleAttribute.HORIZONTAL_SCROLLBAR, new Object[0]);
            if (null != scrollBar1 && null != scrollBar2) {
                scrollBar1.valueProperty().bindBidirectional(scrollBar2.valueProperty());
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }

    public void showAddArriveFactoryDialog() {
        CommonDialog.loadFXMLDialog(ApplicationContext.commonBundle.getString("common_add"), "ArriveFactoryEdit", "ArriveFactory", new Object[0]).showAndWait();
    }

    public void print() {
        try {
            Region region = (Region)(new CustomerFXMLLoader()).load(CommonDialog.class.getResourceAsStream("/views/printpages/QueuedTickets.fxml"));
            PrintUtils.printNode(region, 375, 382, 203, 1);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public static void main(String[] args) {
        PlatformImpl.startup(() -> {
            Region region = null;

            try {
                region = (Region)(new CustomerFXMLLoader()).load(CommonDialog.class.getResourceAsStream("/views/printpages/GradingTickets.fxml"));
            } catch (IOException var2) {
                var2.printStackTrace();
            }

            PrintUtils.printNode(region, 375, 382, 203, 1);
        });
    }

    private static void printImage(BufferedImage image) {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PageFormat pf = printJob.defaultPage();
        Paper paper = pf.getPaper();
        double width = 375.0;
        double height = 382.0;
        paper.setSize(width, height);
        paper.setImageableArea(0.0, 0.0, width, height);
        pf.setOrientation(1);
        pf.setPaper(paper);
        PageFormat validatePage = printJob.validatePage(pf);
        printJob.setPrintable(new MyPrintable(image), validatePage);

        try {
            printJob.print();
        } catch (PrinterException var10) {
            var10.printStackTrace();
        }

    }

    protected static double fromPPItoCM(double dpi) {
        return dpi / 72.0 / 0.393700787;
    }

    protected static double fromCMToPPI(double cm) {
        return toPPI(cm * 0.393700787);
    }

    protected static double toPPI(double inch) {
        return inch * 72.0;
    }

    public static BufferedImage binaryImage(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        BufferedImage binaryImage = new BufferedImage(width, height, 12);

        for(int y = 0; y < height; ++y) {
            for(int x = 0; x < width; ++x) {
                int rgb = image.getRGB(x, y);
                binaryImage.setRGB(x, y, rgb);
            }
        }

        return binaryImage;
    }

    static class MyPrintable implements Printable {
        BufferedImage image;

        public MyPrintable(BufferedImage bufferedImage) {
            this.image = bufferedImage;
        }

        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            int result = 1;
            if (pageIndex < 1) {
                Graphics2D g2d = (Graphics2D)graphics;
                double width = pageFormat.getImageableWidth();
                double height = pageFormat.getImageableHeight();
                System.out.println("Print Size = " + ArriveFactoryController.fromPPItoCM(width) + "x" + ArriveFactoryController.fromPPItoCM(height));
                g2d.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());
                BufferedImage scaled = null;
                scaled = (BufferedImage)this.image.getScaledInstance((int)Math.round(width), (int)Math.round(height) / 2, 4);
                g2d.drawImage(scaled, 0, 0, (ImageObserver)null);

                try {
                    ImageIO.write(scaled, "jpg", new File("f://222.jpg"));
                } catch (IOException var12) {
                    var12.printStackTrace();
                }

                result = 0;
            }

            return result;
        }
    }
}
